package cl.solidit.geomanagement.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;


public class AuthenticationFilter implements Filter {

   

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest r=(HttpServletRequest)request;
		System.out.println(r.getRequestURI());
		String username=(String)r.getSession().getAttribute("username");
		if(true || r.getRequestURI().contains("/resources/") ||( username!=null && username.trim().length()!=0))
			chain.doFilter(request, response);
		else{
			r.getSession().getServletContext().getRequestDispatcher(
		                "/login").forward(request,response);
		}
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	

}
